package ag.webpage;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.io.File;
import java.util.List;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 10:50:00 2018/11/27
 * @version:2.0
 */
public class WebPage {
    public static String url = "https://www.ag8699.com/game/show/agin";
    public static int wait = 1000;

    /***线程等待，等待页面js加载，模块窗弹出**/
    public static void waitPage(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            System.out.println("线程等待出错");
        }
    }

    /**
     * 验证码处理
     **/
    public static void verification(WebDriver driver) {
        waitPage(wait);
        //查找验证码
        WebElement modle = driver.findElement(By.id("tryplayModal"));
        List<WebElement> list = modle.findElements(By.tagName("img"));
        String yanzhengma = "";
        for (WebElement webElement : list) {
            String alt = webElement.getAttribute("alt");
            if ("captcha".equals(alt)) {
                File srcFile = webElement.getScreenshotAs(OutputType.FILE);
                //验证码文字识别
                yanzhengma = tesseract.Test.getResult(srcFile);
                modle.findElement(By.name("captcha")).sendKeys(yanzhengma);
            }
        }
    }
    /*** 初始化打开页面需要执行得操作**/
    public static void init(WebDriver driver) {
        driver.get(url);
        waitPage(wait);
        driver.findElement(By.linkText("免费试玩")).click();
        verification(driver);
        waitPage(wait);
        driver.findElements(By.linkText("免费试玩")).get(0).click();
        waitPage(wait);
    }

    public static void operation() {
    }

}
